import os, sys
import cherrypy
from jinja2 import Environment, FileSystemLoader



'''
Interfaces to contol the web server interactions
'''



class templateFactory(object):
    '''
    Factory class for template rendering engine
    '''
    def __init__(self, ):
        '''
        Setup 
        '''
        templateDir = 'templates'
        self.env = Environment(loader=FileSystemLoader(os.getcwd() + '/' + templateDir))
    
    
    
    def __getTemplate(self, templateName='error.html'):
        '''
        Return Error page if template is not found...
        '''
         
        return self.env.get_template(templateName)
    
    def render(self, template_name, data):
        template = self.__getTemplate(template_name)
        
        return template.render(data)


class webServer:
    '''
    Abstraction for web interface
    
    Adapter class to shield the application from the type os web server 
    used in this application. 
    
    '''
    def __init__(self):
        self.appName = 'Meeting'
        self.root = None

    def log(self, msg, caller=None):
        '''
        Abstraction for webserver logging...
        '''
        
        if caller:
            msg = "[%s:%s] %s" %(self.appName, caller, msg)
        else:
            msg = "[%s] %s" %(self.appName, msg)
            
            
        cherrypy.log(msg='%s' % (msg), context='', severity=20, traceback=False)


    def setSessionValue(self, name, value):
        '''
        Set values in webserver session
        '''
        self.log('Storing session data name: %s = %s' %(name,value))
        cherrypy.session[name] = value
    
    def getSessionValue(self, name):
        '''
        Return single name value pair from user session
        '''
        self.log('Retrieving  value(s) for %s' %(name))
        
        return cherrypy.session.get(name)

    def dumpSession(self):
        '''
        Return all values currently defined in session cache
        '''
        return cherrypy.session.items()
    

    def bindRoot(self, rootClass):
        '''
        Bind root classes to the tree...
        '''
        self.log('Binding root class to application service domain')
        self.root = rootClass
        
    def startServer(self):
        '''
        turn on the webserver
        '''
        self.log('Starting Web Server')
        
        cherrypy.quickstart(self.root, '/', 'conf/server.cfg')
    
    def logout(self):
        '''
        Clear session and logout
        '''
        cherrypy.lib.sessions.expire()
        '''
        redirect to login
        '''
        raise cherrypy.HTTPRedirect("/")
    
    def redirect(self, location='/'):
        '''
        Redirect to new page
        default location is the root of the server...
        '''
        raise cherrypy.HTTPRedirect(location)
        

        
"""
class template_ui(Environment, FileSystemLoader):
    '''
    Factory class for template rendering engine
    '''
    def __init__(self, ):
        '''
        Setup 
        '''
        templateDir = 'templates'
        self.env = Environment(loader=FileSystemLoader(os.getcwd() + '/' + templateDir))
    
    
    
    def __getTemplate(self, templateName='error.html'):
        '''
        Return Error page if template is not found...
        '''
         
        return self.env.get_template(templateName)
    
    def render(self, template_name, data):
        template = self.__getTemplate(template_name)
        
        return template.render(data)


"""
class serverCtl(object):
    '''
    Cherrypy web server control 
    Note: Web site authentication is handled internally be Cherrypy
    Please review configuration file for authorization configuration
    Defautl authentication overrides are in place to perform custom
    login classes. see web_auth.py for details...
    
    Class 
    '''
    
    
    
    
    
    